package com.eversec.view.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import com.eversec.util.ftp.EncryptConstant;


public class ConfigureMainPanel extends JPanel {

	private static final long serialVersionUID = 3608156666513363227L;
	
	private ConfirueDialog confirueDialog;
	
	//配置参数组件
	private JLabel aesKeyLabel;
	private JComboBox aesKeyComboBox;
	private JLabel messageKeyLabel;
	private JComboBox messageKeyComboBox;
	private JLabel aesOffsetsLabel;
	private JComboBox aesoffsetsComboBox;
	private JLabel hashcodeLabel;
	private JComboBox hashcodeComboBox;
	
	//参数数据集合
	private Vector<String> asekeyVector;
	private Vector<String> messagekeyVector;
	private Vector<String> aesoffsetsVector;
	private Vector<String> hashcodeVector;
	
	//按钮面板组件
	private JButton okBtn;
	private JButton cancelBtn;


	public ConfigureMainPanel(ConfirueDialog confirueDialog) {
		this.confirueDialog = confirueDialog;
		
		initData();
		initUI();
		initAction();
	}

	/**
	 * 初始化data
	 */
	private void initData() {
		asekeyVector = new Vector<String>();
		messagekeyVector = new Vector<String>();
		aesoffsetsVector = new Vector<String>();
		hashcodeVector = new Vector<String>();
		
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(new File(System.getProperty("user.dir") + "/config/encry/params.xml"));
			Element rootElement = document.getRootElement();
			
			initEncryData(rootElement, asekeyVector, "/root/aeskey/key");
			initEncryData(rootElement, messagekeyVector, "/root/messagekey/key");
			initEncryData(rootElement, aesoffsetsVector, "/root/aesoffsets/key");
			initEncryData(rootElement, hashcodeVector, "/root/hashcode/key");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	/**
	 * chush
	 * @param rootElement
	 * @param asekeyVector
	 * @param xPath
	 * @throws JDOMException
	 */
	@SuppressWarnings("unchecked")
	private void initEncryData(Element rootElement, Vector<String> asekeyVector, String xPath) throws JDOMException {
		List<Element> encryEles = XPath.selectNodes(rootElement, xPath);
		Iterator<Element> iterEles = encryEles.iterator();
		
		while (iterEles.hasNext()) {
			Element nextEle = iterEles.next();
			String value = nextEle.getAttributeValue("value");
			asekeyVector.add(value);
		}
	}

	/**
	 * 初始化界面UI
	 */
	private void initUI() {
		setLayout(new BorderLayout());
		
		add(initCenterPanel(), BorderLayout.CENTER);
		add(initBtnPanel(), BorderLayout.SOUTH);
	}

	/**
	 * 初始化center panel
	 */
	private JPanel initCenterPanel() {
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		
		aesKeyLabel = new JLabel("aesKey :");
		aesKeyComboBox = new JComboBox(asekeyVector);
		aesKeyComboBox.setEditable(true);
		aesKeyComboBox.setSelectedItem(EncryptConstant.aesKey);
		messageKeyLabel = new JLabel("messageKey :");
		messageKeyComboBox = new JComboBox(messagekeyVector);
		messageKeyComboBox.setEditable(true);
		messageKeyComboBox.setSelectedItem(EncryptConstant.messageKey);
		aesOffsetsLabel = new JLabel("aesOffsets :");
		aesoffsetsComboBox = new JComboBox(aesoffsetsVector);
		aesoffsetsComboBox.setEditable(true);
		aesoffsetsComboBox.setSelectedItem(EncryptConstant.aesOffsets);
		hashcodeLabel = new JLabel("hashcode :");
		hashcodeComboBox = new JComboBox(hashcodeVector);
		hashcodeComboBox.setEditable(true);
		hashcodeComboBox.setSelectedItem(EncryptConstant.hashcode);
		
		centerPanel.add(aesKeyLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 10, 5, 10), 0, 0));
		centerPanel.add(aesKeyComboBox, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(5, 10, 5, 10), 0, 0));
		centerPanel.add(messageKeyLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 10, 5, 10), 0, 0));
		centerPanel.add(messageKeyComboBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(5, 10, 5, 10), 0, 0));
		centerPanel.add(aesOffsetsLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 10, 5, 10), 0, 0));
		centerPanel.add(aesoffsetsComboBox, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(5, 10, 5, 10), 0, 0));
		centerPanel.add(hashcodeLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 10, 0, 10), 0, 0));
		centerPanel.add(hashcodeComboBox, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(5, 10, 0, 10), 0, 0));
		
		return centerPanel;
	}
	
	/**
	 * 初始化按钮面板
	 * @return
	 */
	private JPanel initBtnPanel() {
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		
		okBtn = new JButton("确定");
		cancelBtn = new JButton("取消");
		
		Box mainBox = Box.createHorizontalBox();
		mainBox.add(okBtn);
		mainBox.add(Box.createHorizontalStrut(20));
		mainBox.add(cancelBtn);
		mainBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		
		btnPanel.add(mainBox);
		
		return btnPanel;
	}

	/**
	 * 添加监听
	 */
	private void initAction() {
		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				EncryptConstant.aesKey = aesKeyComboBox.getSelectedItem().toString();
				EncryptConstant.messageKey = messageKeyComboBox.getSelectedItem().toString();
				EncryptConstant.aesOffsets = aesoffsetsComboBox.getSelectedItem().toString();
				EncryptConstant.hashcode = hashcodeComboBox.getSelectedItem().toString();
				JOptionPane.showMessageDialog(ConfigureMainPanel.this, "保存成功！", "", JOptionPane.INFORMATION_MESSAGE);
				confirueDialog.dispose();
			}
		});
		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				confirueDialog.dispose();
			}
		});
	}
	
}
